package com.yang.jxj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.jxj.entity.*;
import com.yang.jxj.service.impl.BonusServiceImpl;
import com.yang.jxj.service.impl.RulesServiceImpl;
import com.yang.jxj.service.impl.StudentsServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

import static com.yang.jxj.Utils.AesUtil.encrypt;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang
 * @since 2024-06-23
 */
@RestController
@RequestMapping("/api/rules")
public class RulesController {

    @Autowired
    private RulesServiceImpl rule;
    @Autowired
    private StudentsServiceImpl stu;
    @Autowired
    private BonusServiceImpl bonu;

    @GetMapping("/test")
    public String test() {
        Rules byId = rule.getById(2);
        return byId.getCriteria();
    }

    @GetMapping("/message")
    public ResponseEntity<HashMap<String, Object>> message(HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<>();
        Integer userId = Integer.valueOf(String.valueOf(request.getAttribute("userId")));
        Students stud = stu.getById(userId);
        QueryWrapper<Bonus> boWrapper = new QueryWrapper<>();
        boWrapper.eq("student_id", userId);
        List<Bonus> list = bonu.list(boWrapper);
        QueryWrapper<Rules> wrapper = new QueryWrapper<Rules>();
        wrapper.eq("year", stud.getEnrollmentYear());
        wrapper.eq("grade", stud.getGrade());
        Rules ruleOne = rule.getOne(wrapper);
        map.put("student", stud);
        map.put("rule", ruleOne);
        map.put("list", list);
        return ResponseEntity.ok(map);
    }

    @GetMapping("/getmsg")
    public HashMap<String, Object> getmsg(@RequestParam Integer userId) {
        HashMap<String, Object> map = new HashMap<>();
        Students stud = stu.getById(userId);
        QueryWrapper<Rules> wrapper = new QueryWrapper<Rules>();
        wrapper.eq("year", stud.getEnrollmentYear());
        wrapper.eq("grade", stud.getGrade());
        Rules ruleOne = rule.getOne(wrapper);
        map.put("student", stud);
        map.put("rule", ruleOne);
        return map;
    }


    @GetMapping("/show")   //规则分页查询
    public IPage<Rules> findPage1(@RequestParam Integer pageNum,
                                  @RequestParam Integer pageSize,
                                  @RequestParam String year) {
        Page<Rules> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Rules> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("year", year);
        objectQueryWrapper.orderByDesc("year");
        return rule.page(page, objectQueryWrapper);
    }

    @PostMapping("/saveR") //学生修改新增
    public HashMap<String,Object> saveS(@RequestBody Rules rules){
        HashMap<String,Object> map = new HashMap<>();

            boolean save = rule.saveOrUpdate(rules);
            if(save ){map.put("code",0);
                map.put("msg","成功");
                return map;
            }else{
                map.put("code",1);
                map.put("msg","失败");
                return map;
            }


    }
}